package jespeak.rules;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LGroup implements Group {

	private char name;
	private List rules;

	public LGroup(char l) {
		name = l;
		rules = new ArrayList<Rule>();
	}
	
	public boolean addRule(Rule r) {
		return rules.add(r);
	}

	@Override
	public String toString() {
		String s = "" + name;
		return s;
	}

	public MatchedRule match(String s, int position) {
		Collections.sort(rules);
		int nrules = rules.size();
		int max = 0, i;
		MatchedRule winner = null;
		for (i = 0; i < nrules; ++i) {
			Rule r = (Rule) rules.get(i);
//			System.out.println(r);
			MatchedRule mr = r.match(s, position);
			if (mr == null) continue;
			int score = mr.getScore();
			System.out.println("\tpropable match: " + score + " " +
						  mr.getRule());
			if (score > max) {
				max = score;
				winner = mr;
				System.out.println("\tL match: " + score + " " +
						  mr.getRule());
			}
		}
		return winner;
	}

}
